import BaseScene from "./core/BaseScene";
import ResManager from "./core/ResManager";
import SceneNavigator from "./core/SceneNavigator";
import UIManager from "./core/UIManager";
import MathUtil from "./utils/MathUtil";
import ObjectUtil from "./utils/ObjectUtil";
import { Utils } from "./utils/Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Game extends BaseScene {

    @property({ type: cc.Label, tooltip: CC_DEV && '加载进度' })
    public labProgress: cc.Label = null;

    @property({ type: cc.Node, tooltip: CC_DEV && 'ui加载进度' })
    public UILoading: cc.Node = null;

    @property({ type: cc.Node, tooltip: CC_DEV && '资源加载进度' })
    public Loading: cc.Node = null;

    private resNum: number;//资源数

    onLoad(): void {
        super.onLoad();
        SceneNavigator.setHome('mainScene');
    }

    protected start(): void {
        this.loadRes();
    }

    //加载资源
    loadRes() {
        let obj = {
            resourceId: "sourceList",
            resourcePath: 'config/sourceList',
            resourceType: 'csv',
            key: ["resourceId"],
        }

        this.Loading.active = true;
        this.resNum = 0;
        ResManager.ins().load(obj, () => {
            let cfg = ResManager.ins().getRes("sourceList");
            let list = ObjectUtil.objConvertArray(cfg);
            this.resNum = list.length;
            this.loadingRes(list, 0);
        }, this.node);
    }

    loadingRes(list: any[], idx: number): void {
        if (list[idx]) {
            ResManager.ins().load(list[idx], () => {
                idx++;
                let _progress = MathUtil.getProgress(idx / this.resNum);
                this.labProgress.string = _progress + "%";
                this.loadingRes(list, idx);
            }, this.node);
        } else {
            cc.log("资源加载完毕");
            this.Loading.active = false;
            this.labProgress.string = "100%";
            UIManager.ins().OpenUI('prefabs/HallView');
        }
    }

}